/* 请求接口baseUrl */
const baseUrl = 'xxxxxx'
const token = uni.getStorageSync('token')
let header = {
	token: token || ''
}
	/* 获取Data */
	export const getData = (url,queryData) => {
		const promise = new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + url,
				data: {
					...queryData
				},
				header: header,
				method: 'GET',
				success(res) {
					if (res.data.code == 0) {
						console.log('接口请求成功',res)
						resolve(res.data)
					}else if (res.data.code == 300){
						console.log('接口请求成功无数据',res)
						resolve(res.data)
					}else{
						console.log('接口请求失败:' + res.data.code + res.data.msg,'/接口地址：' + baseUrl + url,'token',header)
						reject(res)
					}
				}
			})
		})
		return promise.then(res =>{
			  return res
		}).catch(err => {
			console.log(err,'查询shibai接口信息')
			if( err.data.code === 10021) {
				uni.reLaunch({
					url: '/pages/login/login'
				})
			}
			return err
		})
	}
	/* 获取listData */
	export const getListData = (url, queryData) => {
		const promise = new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + url,
				data: {
					...queryData
				},
				method: 'GET',
				success(res) {
					if (res.data.code == 0) {
						console.log('code 0 数据返回',res.data)
						resolve(res.data)
					}else{
						console.log('接口请求失败','参数' + JSON.stringify(queryData))
						reject(res)
					}
				}
			})
		})
		return promise.then(res =>{
			return res
		}).catch(err => {
			return err
		})
	}
	export const postData = (url, queryData) =>  {
		console.log('开始获取签名',url,queryData,header,)
		let queryDatas
		if(Array.isArray(queryData)){
			queryDatas = queryData
		}else{
			queryDatas = {...queryData}
		}
		const promise = new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + url,
				data: queryDatas,
				header: header,
				method: 'POST',
				success(res) {
					console.log(res)
					if (res.data.code == 0) {
						console.log('code 0 数据返回',res)
						resolve(res.data)
					}else{
						console.log('接口请求失败:' + res.data.code + res.data.msg,'/接口地址：' + baseUrl + url,'参数' + JSON.stringify(queryData))
						reject(res)
					}
				}
			})
		})
		return promise.then(res =>{
			return res
		}).catch(err => {
			return err
		})
	}
	export const postDataWithQuery = (url, queryData) =>  {
		let query = ""
		Object.keys(queryData).forEach((key, index) => {
		  query += key + '=' + queryData[key] + '&'
		})
		query = query.substr(0, query.length - 1)
		console.log('开始获取签名了')
		const promise = new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + url + `?${query}` ,
				data: {},
				method: 'POST',
				success(res) {
					if (res.data.code == 0) {
						console.log('code 0 数据返回')
						resolve(res.data)
					}else{
						console.log('接口请求失败',JSON.stringify(queryData))
						reject(res)
					}
				}
			})
		})
		return promise.then(res =>{
			return res
		}).catch(err => {
			return err
		})
	}